package com.imooc.mall.controller;

import com.github.pagehelper.PageInfo;
import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.model.request.AddCategoryReq;
import com.imooc.mall.model.request.UpdateCategoryReq;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.service.UserService;
import com.imooc.mall.vo.CategoryVO;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api
public class CategoryController {

    @Autowired
    UserService userService;

    @Autowired
    CategoryService categoryService;

    /**
     * 后台增加分类，管理员才能操作，判断身份
     *
     * @param session HttpSession
     * @return ApiRestResponse
     */
    @PostMapping("admin/category/add")
    public ApiRestResponse addCategory(
            HttpSession session,
            @Valid @RequestBody AddCategoryReq addCategoryReq
    ) {
        // 判断用户是否登录
        User currentUser = (User) session.getAttribute(Constant.USER_SESSION);
        if (currentUser == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        // 判断是否是管理员
        if (!userService.checkAdminUser(currentUser)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.IS_ADMIN);
        }

        categoryService.addCategory(addCategoryReq);
        return ApiRestResponse.success();
    }

    @PostMapping("admin/category/update")
    public ApiRestResponse updateCategory(
            HttpSession session,
            @Valid @RequestBody UpdateCategoryReq updateCategoryReq
    ) {
        // 判断用户是否登录
        User currentUser = (User) session.getAttribute(Constant.USER_SESSION);
        if (currentUser == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        // 判断是否是管理员
        if (!userService.checkAdminUser(currentUser)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.IS_ADMIN);
        }

        Category category = new Category();
        BeanUtils.copyProperties(updateCategoryReq, category);
        categoryService.updateCategory(category);

        return ApiRestResponse.success();
    }

    @PostMapping("admin/category/delete")
    public ApiRestResponse deleteCategory(@RequestParam("id") Integer id) {
        categoryService.deleteCategory(id);
        return ApiRestResponse.success();
    }

    @PostMapping("admin/category/list")
    public ApiRestResponse listCategoriesForAdmin(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @PostMapping("category/list")
    public ApiRestResponse listCategoriesForCustomer() {
        List<CategoryVO> categoryVOList = categoryService.listCategoryForCustomer(0);
        return ApiRestResponse.success(categoryVOList);
    }
}
